package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AddGroup;
import com.ruoyi.system.domain.AddWechat;
import com.ruoyi.system.service.AddGroupService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： AddGroupController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/add/group")
public class AddGroupController {

    @Resource
    private AddGroupService addGroupService;

    //查询
    @PostMapping("/get/add/group")
    public AjaxResult getAddGroup(@RequestBody AddGroup addGroup){
        System.out.println(addGroup + "我是测试sss");
        //创建分页对象
        Page<AddGroup> page = new Page<>();
        //设置页码
        page.setCurrent(addGroup.getCurrent());
        //设置行数
        page.setSize(addGroup.getSize());
        //执行查询
        addGroupService.page(page,new LambdaQueryWrapper<AddGroup>()
                //微信昵称
                .eq(Objects.nonNull(addGroup.getUserId()), AddGroup::getUserId,addGroup.getUserId())
                //审核状态
                .eq(Objects.nonNull(addGroup.getStatus()),AddGroup::getStatus,addGroup.getStatus())
                //逻辑删除
                .eq(AddGroup::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(AddGroup::getCreateTime)
                //添加分页语句
                .last(" limit " + (addGroup.getCurrent() - 1) * addGroup.getSize() +"," + addGroup.getSize())
        );
        page.setTotal(addGroupService.count(new LambdaQueryWrapper<AddGroup>()
                //微信昵称
                .eq(Objects.nonNull(addGroup.getUserId()), AddGroup::getUserId,addGroup.getUserId())
                //审核状态
                .eq(Objects.nonNull(addGroup.getStatus()),AddGroup::getStatus,addGroup.getStatus())
                //逻辑删除
                .eq(AddGroup::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(AddGroup::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/add/group/{id}")
    public AjaxResult deleteAddGroup(@PathVariable(value = "id")Long id){
        //调用Service层删除
        addGroupService.update(null,new UpdateWrapper<AddGroup>().lambda()
                //主键id
                .eq(AddGroup::getId,id)
                //设置逻辑删除为 真
                .set(AddGroup::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }


    //增加
    @PostMapping("/insert/add/group")
    public AjaxResult insertAddGroup(@RequestBody AddGroup addGroup){
        //设置默认值
        addGroup.setCreateBy(SecurityUtils.getUsername());
        addGroup.setCreateTime(new Date());
        addGroup.setUpdateBy(SecurityUtils.getUsername());
        addGroup.setUpdateTime(new Date());
        addGroup.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        addGroupService.save(addGroup);
        //返回结果
        return AjaxResult.success("新增成功");
    }

    //修改
    @PostMapping("/update/add/group")
    public AjaxResult updateAddGroup(@RequestBody AddGroup addGroup){
        //设置默认值
        addGroup.setUpdateTime(new Date());
        addGroup.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        addGroupService.updateById(addGroup);
        //返回结果
        return AjaxResult.success("修改成功");
    }






}
